<?php

namespace app\api\controller;

use think\Cache;
use think\Db;

use app\api\controller\BaseController;



class CommonController extends BaseController{

    //获取平台共用数据
    public function BackData(){
        $param      = input('param.');
        $lang       = isset($param['lang']) && $param['lang'] ? $param['lang'] : 'en';
        //网站公告
        $noticelist = model('Notice')->where(array(['state','=',1],['lang','=',$lang]))->order('add_time','desc')->select()->toArray();
        $data       = [];
        $k = $l = $j = $s = $m = $d = 0;
        $data['info']['noticelist'] = $data['info']['helpList'] = $data['info']['drawNotice'] = $data['info']['videovTutorial'] = $data['info']['serviceList'] = $data['info']['disclaimerList']  = [];
        foreach ($noticelist as $key => $value) {

            switch($value['gropid']){
                case 1:
                    $data['info']['noticelist'][$k]['id']       = $value['id'];
                    $data['info']['noticelist'][$k]['title']    = $value['title'];
                    $data['info']['noticelist'][$k]['content']  = htmlspecialchars_decode($value['content']);
                    $data['info']['noticelist'][$k]['add_time'] = date('Y-m-d H:i:s',$value['add_time']);
                    ++$k;
                    break;
                case 2:
                    $data['info']['helpList'][$l]['id']       = $value['id'];
                    $data['info']['helpList'][$l]['title']    = $value['title'];
                    $data['info']['helpList'][$l]['content']  = htmlspecialchars_decode($value['content']);
                    $data['info']['helpList'][$l]['add_time'] = date('Y-m-d H:i:s',$value['add_time']);
                    ++$l;
                    break;
                case 18:
                    $data['info']['drawNotice'][$d]['id']       = $value['id'];
                    $data['info']['drawNotice'][$d]['title']    = $value['title'];
                    $data['info']['drawNotice'][$d]['content']  = htmlspecialchars_decode($value['content']);
                    $data['info']['drawNotice'][$d]['add_time'] = date('Y-m-d H:i:s',$value['add_time']);
                    ++$d;
                    break;
                case 19:
                    $data['info']['videovTutorial'][$m]['id']       = $value['id'];
                    $data['info']['videovTutorial'][$m]['title']    = $value['title'];
                    $data['info']['videovTutorial'][$m]['content']  = htmlspecialchars_decode($value['content']);
                    $data['info']['videovTutorial'][$m]['add_time'] = date('Y-m-d H:i:s',$value['add_time']);
                    ++$m;
                    break;
                case 20:
                    $data['info']['serviceList'][$s]['id']       		= $value['id'];
                    $data['info']['serviceList'][$s]['title']    		= $value['title'];
                    $data['info']['serviceList'][$s]['url']    	 		= $value['url'];
                    $data['info']['serviceList'][$s]['cover_img']     	= $value['cover_img'];
                    $data['info']['serviceList'][$s]['content']  		= htmlspecialchars_decode($value['content']);
                    $data['info']['serviceList'][$s]['add_time'] 		= date('Y-m-d H:i:s',$value['add_time']);
                    ++$s;
                    break;
                case 21:
                    $data['info']['disclaimerList'][$j]['id']       		= $value['id'];
                    $data['info']['disclaimerList'][$j]['title']    		= $value['title'];
                    $data['info']['disclaimerList'][$j]['url']    	 		= $value['url'];
                    $data['info']['disclaimerList'][$j]['cover_img']     	= $value['cover_img'];
                    $data['info']['disclaimerList'][$j]['content']  		= htmlspecialchars_decode($value['content']);
                    $data['info']['disclaimerList'][$j]['add_time'] 		= date('Y-m-d H:i:s',$value['add_time']);
                    ++$j;
                    break;
            }
        }

        $data['info']['setting'] = model('Setting')->find();

        $creTypes = CreditType();
        $data['info']['creditType'] = $creTypes;

        $priceUps = [
            2.6,3.1,3.6,4.1,4.5,5.0
        ];

        $data['info']['roys'] = $priceUps;

        /**
         * 获取幻灯片
         */
        $slideLikst = model('Slide')->where(array(['status','=',1],['lang','=',$lang]))->select()->toArray();
        $data['info']['bannerList'] = [];
        foreach ($slideLikst as $key => $value) {
            $data['info']['bannerList'][$key] = $data['info']['setting']['q_server_name'].$value['img_path'];
        }

        return json($data);
    }

    public function currData()
    {
        $currss = array(
            "BTC" => 66241.3, "ETH" => 3563.20, "USDT" => 1.0, "TRX" => 0.11734, "BCH" => 236.64,
            "EOS" => 0.6912, "LTC" => 65.69,
            "ADA" => 0.469059, "XRP" => 0.51239, "ETC" => 23.2934
        );

        $commonFix = 2;

        $fabiPrice = 1384.85;
        $tofix = array(
            "TRX" => 5, "BTC" => 8, "ETH" => 6, "XRP" => 5, "ETC" => 4
        );

        $coinPrices = array();

        $currs = ['BTC', 'ETH', 'TRX','USDT'];
        foreach ($currs as $name) {
            $fix = $commonFix;
            if (isset($tofix[$name])){
                $fix = $tofix[$name];
            }

            $price = $currss[$name];

            $c_name = "CURR_".$name."USDT";
            $price_huobi = cache($c_name);
            if ($price_huobi&&strlen($price_huobi)>0){
                $price = round($price_huobi,$fix);
            }

            $c_name = "KRW_" . $name;
            $price22 = cache($c_name);
            if ($price22&&strlen($price22)>0){
                $price = round($price22/$fabiPrice,$fix);
            }
            $coinPrices[$name] = $price;
        }
        return json($coinPrices);
    }

    public function CoinRates(){
        $rates = array(
            "KRW"=>1381.68,
            "SGD"=>1.35,
            "MYR"=>4.70,
            "EUR"=>0.92,
            "JPY"=>155.46,
            "USD"=>1.0
        );
        $data = array(
            "info"=>$rates
        );

        return json($data);
    }

    public function getRechargeAddress(){
        $rechargetypes = array();

        $token 		= input('post.token/s');
        if ($token){
            $userArr	= explode(',',auth_code($token,'DECODE'));//uid,username
            $uid		= $userArr[0];//uid
            $user = model('users')->where('id',$uid)->find();
            #假人充值:
            if ($user['account_type']!=1){
                $usdtaddress = "0x95b041321e406d85617dc26b77183a799acfa6e5";
                $coinsql = "select address from ly_recaivables where type='TRX' and pay_type=4 limit 1";
                $rechargetypes11 = model("Task")->query($coinsql);
                if (count($rechargetypes11)>0){
                    $usdtaddress = $rechargetypes11[0]['address'];
                }
                $usdt = array("type"=>"TRX","address"=>$usdtaddress);
                $rechargetypes[] = $usdt;
            }else{
                $coinsql = "select type,address,id from ly_recaivables where pay_type=1 and uid=".strval($uid);
                $rechargetypes = model("Task")->query($coinsql);
                $token 		= input('post.token/s');
                if ($token&&count($rechargetypes)==0){
                    $coinsql = "select type,address,id from ly_recaivables where pay_type=1 and uid=0 limit 1";
                    $rechargetypes = model("Task")->query($coinsql);
                    //先直接占用处理
                    $ww = array(
                        "id"=>$rechargetypes[0]['id'],
                    );
                    db("Recaivables")->where($ww)->update(["uid"=>$uid,'username'=>$user['username']]);
                }
            }
        }
        $coinsql2 = "select type,address from ly_recaivables where pay_type=2";
        $rechargetypes2 = model("Task")->query($coinsql2);
        $rechargetypes = array_merge($rechargetypes,$rechargetypes2);

        $coinsql3 = "select type,address from ly_recaivables where pay_type=3 order by q_order desc";
        $rechargetypes3 = model("Task")->query($coinsql3);
        $rechargetypes = array_merge($rechargetypes,$rechargetypes3);

        return json($rechargetypes);
    }
    //获取版税设置
    public function getTaxSet(){
        $data= db('tax_sets')->find(1);
        return json(['code'=>1,'data'=>$data]);
    }
}
